package com.InvoicingSystem.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.InvoicingSystem.pojo.Sale;
import com.InvoicingSystem.pojo.UserInfo;
import com.InvoicingSystem.service.SaleServices;
import com.InvoicingSystem.util.Const;

@Controller
@RequestMapping("/sale/")
public class SaleController {

	@Autowired
	private SaleServices saleServices;

	@RequestMapping("addSale")
	public String addSale(Sale sale, HttpSession session) {
		UserInfo userInfo = (UserInfo) session.getAttribute(Const.USER_INFO);
		// 横向越权判断
		if (null == userInfo) {
			return "redirect:/login.jsp";
		}

		// 添加动作
		boolean bool = saleServices.addSale(sale, userInfo);
		if (bool) {

		}

		return "redirect:/index.jsp";
	}
	@RequestMapping("findPageAll")
	public String findPageAll(@RequestParam(value="indexPage",defaultValue="1") Integer indexPage,
			  @RequestParam(value="pageSize",defaultValue="5") Integer pageSize,
			  @RequestParam(value="orderByIndex",defaultValue="1") Integer orderByIndex,
			  Map<String, Object> map,HttpSession session) {
		// 约定orderByIndex 作为排序方式 1按日期排序 2按总价排序
		UserInfo userInfo = (UserInfo) session.getAttribute(Const.USER_INFO);
		if (userInfo == null) {
			return "redirect:/login.jsp";
		}
		List<Map<String, Object>> list = saleServices.findPageAll(indexPage, pageSize, orderByIndex);

		map.put("saleList", list);
		map.put("totalPage", saleServices.findTotalPage(pageSize));
		map.put("totalTable", saleServices.findTotalTable());
		map.put("orderByIndex", orderByIndex);
		map.put("indexPage", indexPage);

		return "list";

	}

}
